/**
 * 
 */
package py.edu.ucom.cadira.net.serializables;

import org.jdom.Document;
import org.jdom.Element;
import py.edu.ucom.cadira.game.war.Board;
import py.edu.ucom.cadira.logic.Mesa;
import py.edu.ucom.cadira.net.XMLSerializable;

/**
 * @author largonet
 *
 */
public class SetBoardRequest implements XMLSerializable {
    private static final String NODE_SET_BOARD = "setBoard";
    private Mesa mesa;
    private Board board;
    
    public SetBoardRequest(Mesa mesa, Board board) {
        this.mesa = mesa;
        this.board = board;
    }

    public SetBoardRequest() {
        this.mesa = new Mesa();
        this.board = new Board();
    }

    public Board getBoard() {
        return board;
    }

    public void setBoard(Board board) {
        this.board = board;
    }
    
    public Mesa getMesa() {
        return mesa;
    }

    public void setMesa(Mesa mesa) {
        this.mesa = mesa;
    }
    
    public void fromXML(Document xml) {
        // pido el root del XML (CADIRA)
        Element root = xml.getRootElement();
        Element setboard = root.getChild(NODE_SET_BOARD);
        Element tableNode = setboard.getChild("table");
        mesa.setMesaId(Integer.parseInt(tableNode.getAttributeValue("id")));
        Element statusNode = tableNode.getChild("status");
        mesa.setStatus(Boolean.valueOf(statusNode.getValue()));
        Element tableroNode = tableNode.getChild("tablero");
        board.fromXml(tableroNode);
    }

    public void toXML(Element root) {
        // arma la cabecera de la tabla
        Element joinTableNode = new Element(NODE_SET_BOARD);
        Element tableNode = new Element("table");
        tableNode.setAttribute("id", mesa.getMesaId().toString());
        Element statusNode = new Element("status");
        statusNode.addContent(String.valueOf(mesa.isStatus()));
        tableNode.addContent(statusNode);
        Element tableroNode = new Element("tablero");
        board.toXml(tableroNode);
        tableNode.addContent(tableroNode);
        joinTableNode.addContent(tableNode);
        root.addContent(joinTableNode);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof SetBoardRequest)) {
            return false;
        }
        SetBoardRequest otherObj = (SetBoardRequest) obj;
        if (otherObj.getMesa().getMesaId().equals(getMesa().getMesaId())) {
            return true;
        }
        return false;
    }

}
